package com.zj.leetcode.easy.hash;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @program: algorithm
 * @description: 两个列表的最小索引总和
 * 599 minimum-index-sum-of-two-lists
 * @author: Zhang Bo
 * @create: 2022-01-14 17:05
 **/
public class MinimumIndexSumOfTwoLists {
    public static void main(String[] args) {
        Solution solution = new MinimumIndexSumOfTwoLists().new Solution();
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public String[] findRestaurant(String[] list1, String[] list2) {

            HashMap<String, Integer> map = new HashMap<>();
            for (int i = 0; i < list1.length; i++) {
                map.put(list1[i], i);
            }

            int min = Integer.MAX_VALUE;

            List<String> list = new ArrayList<>();
            for (int i = 0; i < list2.length; i++) {
                if (map.containsKey(list2[i])) {

                    int temp = map.get(list2[i]) + i;
                    if (temp < min) {
                        min = temp;
                        list.clear();
                        list.add(list2[i]);
                    } else if (temp == min) {
                        list.add(list2[i]);
                    }

                }
            }

            return list.toArray(new String[list.size()]);
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}
